package de.placidi.pressreview.parsing;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * Has some useful methods for link filtering. 
 * 
 * @author Alexander Placidi
 *
 */
public final class HTMLParserHelper {
		
	private HTMLParserHelper() {}
	
	public static boolean isContained(List<String> links, String link) {
		for (String l : links) {
			if (l.startsWith(link)) return true;
		}
		
		return false;
	}
	
	/**
	 * Checks, if the scanner is still on the site. 
	 * 
	 * @param link
	 * @param filter
	 * @return
	 */
	public static boolean isDomain(String link, String filter) {
		Boolean isDomain = null;
		isDomain = link.contains(filter);
		return isDomain;
	}
	
	/**
	 * Checks, if the site is in english. 
	 * 
	 * @param htmlDoc
	 * @return
	 */
	public static boolean isEnglish(Document htmlDoc) {
		Boolean isLanguage = false;
		
		Elements elements = htmlDoc.select("html[xml:lang]");
		for (Element element : elements) {
			String lang = element.attr("xml:lang");
			if(lang.equalsIgnoreCase("en") | lang.equalsIgnoreCase("en-gb")) {	
				isLanguage = true;
			}
		}
		
		return isLanguage;
	}
	
	/**
	 * Checks, if the link contains unwanted characters. 
	 * 
	 * @param link
	 * @return
	 */
	public static boolean isClean(String link) {
		Boolean isClean = true;
		
		List<String> dirt = new ArrayList<String>();
		dirt.add("%");
		dirt.add("#");
		dirt.add("?");
		dirt.add("&");
		dirt.add("stm");
		
		for (String d : dirt) {
			if (link.contains(d)) {
				isClean = false;
			}
		}
		
		return isClean;
	}
	
	public static void writeFile(List<String> lines) throws IOException {
		 BufferedWriter out = new BufferedWriter(new FileWriter("alllinks.txt", true));
        
		 for (String line : lines) {
			out.write(line+"\n");
		 }
		 
        out.close();
	}
	
}
